package com.imelite.qyh.filter;

import com.imelite.qyh.factory.EliteCRMView_OrgUserFactory;
import com.imelite.qyh.model.EliteCRMView_OrgUser;
import com.imelite.qyh.util.BaseStringUtil;
import com.imelite.qyh.util.BaseUserUtil;
import com.imelite.qyh.util.CookieUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class QyhFilter implements Filter {
    private static final Log log = LogFactory.getLog(QyhFilter.class);
    @Override
    public void init(FilterConfig filterConfig) throws ServletException { }

    @Override
    public void destroy() {}

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse res = ((HttpServletResponse) servletResponse);


        //如果在白名单中就放行
        String url=req.getRequestURL().toString();//请求路径
        String checkUrl=url.substring(url.indexOf("/com"),url.lastIndexOf(".do"));
        if(checkUrl(checkUrl)){
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }

        String WxUserId=BaseUserUtil.getWxUserId(req);
        EliteCRMView_OrgUser user= EliteCRMView_OrgUserFactory.selectByWxcode(WxUserId);
        if(BaseStringUtil.isBlank(user.getId())){
            String params=req.getQueryString();//请求参数
            String AgentId=req.getParameter("AgentId");
            String login_url="/com/imelite/qyh/UserAction/setUserWxInfo.do?WxUserId="+WxUserId+"&url="+url+"&params="+params+"&AgentId="+AgentId;
            res.sendRedirect(login_url);
        }

        //放行
        filterChain.doFilter(servletRequest, servletResponse);
    }

    //放行地址表
    private boolean checkUrl(String url){
        List<String> urllist=new ArrayList<>();
        urllist.add("/com/imelite/qyh/UserAction/updateUser");
        urllist.add("/com/imelite/qyh/UserAction/getUserByLogin");
        urllist.add("/com/imelite/qyh/EliteView_YyxxAction/yyxxByTelPage");
        urllist.add("/com/imelite/qyh/EliteView_YyxxAction/ajax_getYyxxByForm");


        urllist.add("/com/imelite/qyh/UserAction/setUserWxInfo");
        urllist.add("/com/imelite/qyh/JxcpcsAction/getTestPage");
        urllist.add("/com/imelite/qyh/JxcpcsAction/getAccessPage");
        urllist.add("/com/imelite/qyh/YycpbgAction/insert");
        urllist.add("/com/imelite/qyh/JxcpcsAction/getTest2");
        urllist.add("/com/imelite/qyh/JxcpcsAction/getTest");
        urllist.add("/com/imelite/qyh/JxcpcsAction/getSubmitAccessPage");
        urllist.add("/com/imelite/qyh/JxcpcsAction/getAccessPageToPrint");
        urllist.add("/com/imelite/qyh/EliteView_YyxxAction/update_idowner");
        urllist.add("/com/imelite/qyh/YycpbgAction/update");

        for(String s:urllist){
            if(s.equals(url)){
                return true;
            }
        }

        return false;
    }


}
